<template>
  <div class="container">
    <div v-if="qrCode.startsWith('msg=')" class="qrcode" @click="refreshQrcode">
      {{ qrCode.replace('msg=', '') }}
    </div>
    <img v-else :src="qrCode" alt="正在加载" class="qrcode" @click="refreshQrcode" />
    <div style="font-size: 12px">点击图片刷新二维码</div>
    <div style="font-size: 12px">已有 {{ count }} 人次使用成功</div>
    <div style="height: 50px"></div>
    <div class="cookie-container">
      <p>GTK:</p>
      <p class="cookie">{{ gtk }}</p>
    </div>
    <div class="cookie-container">
      <p>Cookies:</p>
      <p class="cookie">{{ cookie }}</p>
    </div>

    <p class="copyrite">若有使用问题，请联系小娟</p>
    <p class="copyrite">powered by Gou xiaojuan ©️ 2025</p>
  </div>
</template>

<script setup>
import { onMounted, onBeforeUnmount, ref } from "vue";
import axios from "axios";
const qrCode = ref('msg=点击获取二维码');
const cookie = ref('');
const gtk = ref('');
let abortController = null;
let shouldContinue = true;
let count=ref(0)
let base_url='http://117.50.162.33:9090'
// let base_url='http://192.168.1.109:9090'

async function add_count() {
  await axios.get(`${base_url}/add_count`);
  const res = await axios.get(`${base_url}/get_count`);
  count.value=res.data
}
const makeRequest = async () => {
  if (!shouldContinue) return;
  if (abortController) abortController.abort();
  abortController = new AbortController();
  const { signal } = abortController;



  try {
    const response = await axios.get(`${base_url}/check`, { signal });
    if (response.data.code === 2) {
      shouldContinue = false;
      abortController.abort();
      qrCode.value = 'msg=您的帐号非服务商，请点击刷新重扫';
    }
    if (response.data.code === 0) {
      qrCode.value = 'msg=已扫描';
      const d = response.data.data;
      console.log(d)
      gtk.value = d['gtk'];
      cookie.value = `gdt_refer=sso.e.qq.com; gdt_full_refer=https%3A%2F%2Fsso.e.qq.com%2F; gdt_token=${d['gdt_token']}; gdt_protect=${d['gdt_protect']}; bm_id=${d['bm_id']}; trace_user_id=${d['trace_user_id']}; trace_agency_id=${d['trace_agency_id']}; trace_company_id=${d['trace_company_id']}; agency_uid=${d['agency_uid']}`;
      shouldContinue = false;
      abortController.abort();
      add_count();
    }
  } catch (e) {
  } finally {
    if (shouldContinue) {
      setTimeout(makeRequest, 3000);
    }
  }
};
async function refreshQrcode() {
  try {
    gtk.value = '';
    cookie.value = '';
    shouldContinue = false;
    if (abortController) abortController.abort();
    const response = await axios.get(`${base_url}/get_img`);
    const res = await axios.get(`${base_url}/get_count`);
    count.value=res.data
    qrCode.value = response.data;
    shouldContinue = true;
    makeRequest();
  } catch (error) {
    console.error('请求失败:', error);
  }
}
onMounted(refreshQrcode);
onBeforeUnmount(() => {
  shouldContinue = false;
  if (abortController) abortController.abort();
});
</script>
<style scoped>
.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.qrcode {
  width: 134px;
  height: 134px;
  margin-bottom: 12px;
  background-color: #f2f3f5;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 5px;
}
.cookie-container {
  position: relative;
  width: 90vw;
  margin: 10px;
}
.cookie {
  background-color: #f2f3f5;
  padding: 20px 10px;
  border-radius: 10px;
  word-break: break-all;
  overflow-wrap: break-word;
}
.copyrite{
  color: darkgrey;
  font-size: 13px;
}
</style>
